//
//  LXActivationViewController.m
//  INMOTION_CustomMadeProject
//
//  Created by cjg on 2021/3/25.
//  Copyright © 2021 cjg. All rights reserved.
//

#import "LXActivationViewController.h"
#import "NSObject+HUD.h"
#import "KSLanguageManager.h"
#import "IMTHTTPSessionManager.h"
#import "IMTConfig.h"
#import "KSBLEManager.h"
#import "IMTWebViewController.h"
#import "LXCustom.h"
#import "AppTypeTool.h"
#import "CarModel.h"

@interface LXActivationViewController ()
@property (weak, nonatomic) IBOutlet UILabel *snLB;
@property (weak, nonatomic) IBOutlet UIImageView *logoIM;
@property (weak, nonatomic) IBOutlet UIButton *urlLinkBtn;
@property (weak, nonatomic) IBOutlet UIButton *readBtn;
@property (weak, nonatomic) IBOutlet UILabel *protocolLB;
@property (weak, nonatomic) IBOutlet UIButton *activationBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *snMargin;
@property (nonatomic , assign) BOOL isSelectProtocal;
@end

@implementation LXActivationViewController

- (void)backAction {
    [[KSBLEManager sharedManager] disconnect];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    self.title = IMTNewCarLocalizedString(@"btn_activate");
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back_white"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = left;

    self.snLB.text = self.carModel.lx_sn;
    self.isSelectProtocal = NO;
    [self.urlLinkBtn setTitle:IMTNewCarLocalizedString(@"operation_guidelines") forState:(UIControlStateNormal)];
    [self.activationBtn setTitle:IMTNewCarLocalizedString(@"btn_activate") forState:(UIControlStateNormal)];
    self.protocolLB.text = IMTNewCarLocalizedString(@"I have read the agreement and agreed");
    
    self.activationBtn.layer.cornerRadius = 22.5;
    self.activationBtn.layer.masksToBounds = YES;
    
    self.activationBtn.userInteractionEnabled = NO;
    self.activationBtn.backgroundColor = kIMTDefaultLineColor;
    
    // 隐藏操作指引
    if (![LXCustom sharedCustom].showOperationGuide) {
        self.urlLinkBtn.hidden = YES;
        self.readBtn.hidden = YES;
        self.protocolLB.hidden = YES;
        
        self.activationBtn.userInteractionEnabled = YES;
        self.activationBtn.backgroundColor = [AppTypeTool getThemeColor];
    }
    // 导航控制器view从0开始计算
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    
    
    if (IMTCarSeriesE20(self.carModel.LX_CarType)) {
        [self.logoIM setImage:[UIImage imageNamed:@"E20"]];
    } else {
        [self.logoIM setImage:[UIImage imageNamed:@"img_l9"]];
    }
}

// 激活按钮
- (IBAction)activationBtnAction:(UIButton *)sender {
    BOOL isE20 = IMTCarSeriesE20(self.carModel.LX_CarType);
    [self showProgressHUDWithText:IMTNewCarLocalizedString(@"prompt_loading")];
    [self.carModel sendCarActivationWith:[NSDate date] target: isE20 ? 0x21 : 0x32 isE20: isE20 successBlock:^{
        [self.carModel writeDataWithCmdType:36 uint8Param:2 successBlock:^{
            [self showSuccessHUDWithText:IMTNewCarLocalizedString(@"prompt_activate_success")];
            [self activationCarFromeServer];
            [self.navigationController popViewControllerAnimated:YES];
        } failBlock:^(NSError *error) {
            [self showErrorHUDWithText:IMTNewCarLocalizedString(@"prompt_activate_failed")];
            [[KSBLEManager sharedManager] disconnect];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    } failBlock:^(NSError *error) {
        [self showErrorHUDWithText:IMTNewCarLocalizedString(@"prompt_activate_failed")];
        [[KSBLEManager sharedManager] disconnect];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)activationCarFromeServer {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    
    NSDictionary *param = @{@"sn":self.carModel.lx_sn,@"carType":self.carModel.LX_CarType,@"activeTime":currentDateStr};
    [[IMTHTTPSessionManager sharedInstance] ks_POST:@"activateOemCar" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"====== 车辆激活成功");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"====== 车辆激活失败：%@", error);
    }];
}

// 同意协议
- (IBAction)hasReadProtocalAction:(UIButton *)sender {
    self.isSelectProtocal = !self.isSelectProtocal;
    
    if (self.isSelectProtocal) {
        [self.readBtn setImage:[UIImage imageNamed:@"agreeyes"] forState:(UIControlStateNormal)];
        self.activationBtn.backgroundColor = [AppTypeTool getThemeColor];
    }else {
        [self.readBtn setImage:[UIImage imageNamed:@"agreenot"] forState:(UIControlStateNormal)];
        self.activationBtn.backgroundColor = kIMTDefaultLineColor;
    }
    self.activationBtn.userInteractionEnabled = self.isSelectProtocal;
}

// 链接点击
- (IBAction)rulLinkBtnAction:(UIButton *)sender {
    IMTWebViewController *web = [[IMTWebViewController alloc] init];
    web.url = IMTNewCarLocalizedString(@"Scooter url");
    [self.navigationController pushViewController:web animated:YES];

}


@end
